#include "websocket_handler.h"
#include "database.h"
#include <libwebsockets.h>

int main(void) {
    if (!init_database()) {
        return 1;
    }

    struct lws_context_creation_info info;
    struct lws_context *context;
    const int port = 8888;

    memset(&info, 0, sizeof(info));
    info.port = port;
    info.protocols = protocols;
    info.gid = -1;
    info.uid = -1;
    info.options = 0;

    context = lws_create_context(&info);
    if (!context) {
        fprintf(stderr, "创建libwebsockets上下文失败\n");
        close_database();
        return -1;
    }

    printf("WebSocket服务器启动，监听端口 %d\n", port);

    while (1) {
        lws_service(context, 50);
    }

    lws_context_destroy(context);
    close_database();
    return 0;
}